package com.qf.commons.domain.base;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一返回的对象
 */
@Data
@Accessors(chain = true)
@Schema(description = "统一返回对象")
public class R<T> implements Serializable {

    @Schema(description = "统一返回状态码")
    private Integer code;
    @Schema(description = "统一返回描述信息")
    private String msg;
    @Schema(description = "数据部分")
    private T data;

    public static <S> R succ(S data){
        return new R()
                .setCode(Codes.SUCC.getCode())
                .setMsg(Codes.SUCC.getMsg())
                .setData(data);
    }

    public static R succ(){
        return succ(null);
    }

    public static <S> R fail(Codes codes, S data){
        return new R()
                .setCode(codes.getCode())
                .setMsg(codes.getMsg())
                .setData(data);
    }

    public static <S> R fail(Codes codes){
        return fail(codes, null);
    }
}
